<?
    final class Principio_Model_SpaExtTemplateMapper extends Principio_Mapper
    {
        protected static $db_table = null;
        protected static function getDbTableName() { return "SpaExtTemplates"; }


        public static function delete(Principio_Model_Spa $model_spa)
        {
            try
            {
                self::getDBTable()->delete(['spa_id=?' => intval($model_spa->getID())]);

                return true;
            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                throw new RuntimeException("Couldn't delete templates.");
            };
        }


        public static function update(Principio_Model_Spa $model_spa)
        {
            try
            {
                self::getDBTable()->getAdapter()->beginTransaction();

                foreach ($model_spa->getTemplates() as $model_template)
                    self::getDBTable()->update(['template' => $model_template->getTemplate()], ['spa_id=?' => $model_spa->getID(), 'ext_number=?' => $model_template->getExtNumber()]);

                self::getDBTable()->getAdapter()->commit();

                return true;

            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                self::getDBTable()->getAdapter()->rollBack();

                return false;
            };
        }


        public static function insert(Principio_Model_Spa $model_spa)
        {
            try
            {
                for ($i = 0; $i < $model_spa->getModelType()->getExts() + Principio_Model_Templatable::TEMPLATE_SHIFT; $i++)
                    self::getDBTable()->insert(['template' => "", 'spa_id' => $model_spa->getID(), 'ext_number' => $i]);

            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function fetch(Principio_Model_Spa $model_spa)
        {
            try
            {
                $values = self::getDBTable()->fetchAll(self::getDBTable()->select()->where("spa_id=?", $model_spa->getID()));
                return Principio_Model_Template::makeCollection($values->toArray());
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }

        /*
        public static function search($string)
        {
            $search_fields = array(
                "template" => "Template"
            );
            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);
            $search = array();
            foreach ($result as $res)
            {
                $spa_info = Principio_Model_SpaMapper::get($res["spa_id"]);
                $model_search = new Principio_Model_Search($string, "Spa");
                $menu_data = array("menu_name" => "view_by_company", "level" => EXPAND_VIEW_BY_COMPANY, "type_id" => $spa_info->getTypeID(), "company_id" => $spa_info->getCompanyID(), "open_level" => 2);
                $model_search->setID($res["spa_id"])->setResultName($spa_info->getIP()." [".$spa_info->getMAC()."]")->setOpenItemFunctionName("Spa")->setParentID(false)->setMenuData($menu_data);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                        $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["id"]] = $model_search;
            }
            return $search;
        }
        */
    }
?>